<?php

use yii\db\Migration;

class m170702_043954_backend_table extends Migration
{
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci
            $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
        }


        $this->createTable('{{%admin}}', [
            'id' => $this->primaryKey(),
            'username' => $this->string()->notNull()->unique()->comment('用户名'),
            'auth_key' => $this->string(32)->notNull()->comment('Auth Key'),
            'password_hash' => $this->string()->notNull()->comment('Password Hash'),
            'password_reset_token' => $this->string()->unique()->comment('Password Reset Token'),
            'email' => $this->string()->notNull()->unique()->comment('邮箱'),

            'status' => $this->smallInteger()->notNull()->defaultValue(10)->comment('状态'),
            'created_at' => $this->integer()->notNull()->comment('创建时间'),
            'updated_at' => $this->integer()->notNull()->comment('更新时间'),
        ], $tableOptions);

        $this->createTable('{{admin_role}}',[
            'admin_id' => $this->integer()->unsigned()->comment('用户ID') ,
            'role_id' => $this->integer()->unsigned()->comment('角色ID') ,
        ],$tableOptions) ;

        $this->createIndex('amdin_role','{{admin_role}}',['admin_id','role_id'],true);


        $this->createTable('{{rbac_role_resource}}',[
            'resource_id' => $this->integer()->unsigned()->comment('资源ID') ,
            'role_id' => $this->integer()->unsigned()->comment('角色ID') ,
        ],$tableOptions) ;

        $this->createIndex('rbac_role_resource','{{rbac_role_resource}}',['role_id','resource_id'],true);


        $this->createTable('{{rbac_resource_uri}}',[
            'id' => $this->primaryKey(),
            'resource_id' => $this->integer()->unsigned()->comment('资源ID') ,
            'uri' => $this->string()->defaultValue('')->comment('URI'),
            'method' => $this->string(50)->defaultValue('')->comment('访问方法'),
        ],$tableOptions) ;

        $this->createTable('{{rbac_role}}',[
            'id' => $this->primaryKey(),
            'name' => $this->string(50)->defaultValue('')->comment('角色名'),
        ],$tableOptions) ;

        $this->createTable('{{rbac_resource}}',[
            'id' => $this->primaryKey(),
            'pid' => $this->integer()->unsigned()->defaultValue(0)->comment('上级资源ID'),
            'type'=> $this->smallInteger()->defaultValue(0)->comment('类型'),
            'link' => $this->string()->defaultValue('')->comment('链接地址'),
            'code' => $this->string(50)->unique()->comment('标识符'),
            'name' => $this->string(50)->comment('资源名称'),

        ],$tableOptions) ;

    }

    public function safeDown()
    {
        $this->dropTable('{{admin}}');
        $this->dropTable('{{admin_role}}');
        $this->dropTable('{{rbac_role_resource}}');
        $this->dropTable('{{rbac_resource_uri}}');
        $this->dropTable('{{rbac_role}}');
        $this->dropTable('{{rbac_resource}}');
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m170702_043954_backend_table cannot be reverted.\n";

        return false;
    }
    */
}
